package org.chuan.hisports.controller;


import net.sf.json.JSON;
import org.chuan.hisports.entity.*;
import org.chuan.hisports.service.CoachService;
import org.chuan.hisports.service.FitnessAdminService;
import org.chuan.hisports.service.MemberService;
import org.chuan.hisports.service.NoticeService;
import org.chuan.hisports.util.CreatTime;
import org.chuan.hisports.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller("admin")
@RequestMapping("admin")
public class adminController {
    @Autowired
    NoticeService noticeService;
    @Autowired
    MemberService memberService;
    @Autowired
    CoachService coachService;
    @Autowired
    FitnessAdminService ft;
    //管理员登录
    @RequestMapping("login")
    @ResponseBody
    public JSONResult login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        HttpServletRequest request){
        FitnessAdmin fa = ft.login(username,password);
        if(fa == null){
            return JSONResult.build(200,"登录失败，请检查账号、密码是否正确",null);
        }
        HttpSession hs = request.getSession();
        hs.setAttribute("adminname",username);
        return JSONResult.build(200,"success",null);
    }

    //查看健身房中所有的会员
    @RequestMapping("readUser")
    @ResponseBody
    public JSONResult readUser(){

        List<AdMember> list = memberService.adminSelectMember();
        return JSONResult.build(200,"success",list);
    }
    //查询某个会员的专属教练
    @RequestMapping("readMemcoa")
    @ResponseBody
    public JSONResult readMemcoa(@RequestParam("uid") String uid){
        Map<String,String> map = ft.readMemcoa(uid);
        return JSONResult.build(200,"success",map);
    }
    //解除专属教练绑定
    @RequestMapping("unbindcoach")
    @ResponseBody
    public JSONResult unbindcoach(@RequestParam("uid") String uid,
                                  @RequestParam("cid") String cid){
        ft.deleteMemcoa(uid, cid);
        return JSONResult.build(200,"success",null);
    }
    //绑定专属教练
    @RequestMapping("bindcoach")
    @ResponseBody
    public JSONResult bindcoach(@RequestParam("uid") String uid,
                                @RequestParam("cid") String cid){
        ft.insertMemcoa(uid, cid);
        return JSONResult.build(200,"success",null);
    }
    //给指定会员付费
    @RequestMapping("upLevelById")
    @ResponseBody
    public JSONResult upLevelById(@RequestParam("uid") String uid,
                                  @RequestParam("month") String month){
        memberService.updateLevel(uid,month);
        String time = memberService.selectLastTime(uid);
        return JSONResult.build(200,"success",time);
    }

    //查询某个会员的个人信息
    @RequestMapping("readUserinfo")
    @ResponseBody
    public String readUserinfo(){
        return null;
    }

    //查询健身房所有的教练
    @RequestMapping("readCoach")
    @ResponseBody
    public JSONResult readCoach(){

        List<AdCoach> list = coachService.selectAdCoach();
        return JSONResult.build(200,"success",list);

    }

    //查看教练的个人信息
    @RequestMapping("coachInfo")
    @ResponseBody
    public JSONResult coachInfo(@RequestParam("cid") String cid){
        CoachInfo coachInfo = coachService.selectByCid(cid);
        return JSONResult.build(200,"success",coachInfo);
    }

    //开通教练账号
    @RequestMapping("insertCoach")
    @ResponseBody
    public JSONResult insertCoach(@RequestParam("phone") String phone,
                                  @RequestParam("password") String password){
        //查询账号是否开通
        if(coachService.selectIsExsit(phone)) {
            return JSONResult.build(200, "账号已经激活", null);
        }
        //开通账号
        if(coachService.insertCoach(phone,password)){
            return  JSONResult.build(200,"success",null);
        }
        return JSONResult.build(200,"账号开通失败，请重新开通",null);
    }

    //发布公告信息
    @RequestMapping("insertNotice")
    @ResponseBody
    public JSONResult insertNotice(@RequestParam("title") String title,
                               @RequestParam("type") String type,
                               @RequestParam("content") String content){
        Notice notice = new Notice();
        notice.setTitle(title);
        notice.setType(type);
        notice.setContent(content);
        CreatTime ct = new CreatTime();
        String time = ct.dataTime(ct.creatTime());
        notice.setPublishtime(time);
        noticeService.insert(notice);
        return JSONResult.build(200,"success",null);
    }



    //查看所有的公告与资讯
    @RequestMapping("selectNoticeAll")
    @ResponseBody
    public JSONResult selectNoticeAll(){
        List<Notice> list = noticeService.selectAll();
        return JSONResult.build(200,"success",list);
    }
    //根据id查询资讯内容
    @RequestMapping("selectNoticeByid")
    @ResponseBody
    public JSONResult selectNoticeByid(@RequestParam("id") String id){
        Notice notice = noticeService.selectById(id);
        return JSONResult.build(200,"success",notice);
    }
    //删除公告资讯
    @RequestMapping("deleteNotice")
    @ResponseBody
    public JSONResult deleteNotice(@RequestParam("id") String id){
        noticeService.delete(id);
        return JSONResult.build(200,"success",null);
    }

    //页面跳转
    @RequestMapping("member")
    public String member(){
        return "admins/adminmember";
    }

    @RequestMapping("coachs")
    public String coachs(){
        return "admins/admincoach";
    }
    @RequestMapping("notice")
    public String notice(){
        return "admins/adminnotice";
    }
    @RequestMapping("addcoach")
    public String addcoach(){
        return "admins/adminaddcoach";
    }
    @RequestMapping("addnotice")
    public  String addnotice(){
        return "admins/adminaddnotice";
    }
    @RequestMapping("index")
    public String index(){
        return "admins/adminindex";
    }
}
